package com.jwell.panrank.ui.adapter

import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jwell.panrank.R
import com.jwell.panrank.entity.InlineBean
import kotlinx.android.synthetic.main.item_register_plane_layout.view.*

/**
 * Created by 76577 on 2018/4/11.
 * 预登记验收计划，选择供应商
 */
class RegisterPlaneAdapter constructor(list: ArrayList<InlineBean>) : RecyclerView.Adapter<RegisterPlaneAdapter.ViewHolder>() {

    private var isCheckBoxVisibility = false
    private var type = -1
    private var list = list

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_register_plane_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        with(holder.itemView) {
            when (isCheckBoxVisibility) {
                true -> checkbox.visibility = View.VISIBLE
                else -> checkbox.visibility = View.GONE
            }
            when (list[position].checked) {
                true -> checkbox.isChecked = true
                else -> checkbox.isChecked = false
            }
            checkbox.setOnClickListener {
                when (list[position].checked) {
                    true -> list[position].checked = false
                    else -> list[position].checked = true
                }
            }
            //解决editText数据错乱
            editText.setText("")
            list[position].carNum.let {
                if (it > 0) {
                    editText.setText(it.toString())
                } else {
                    editText.setText("")
                }
            }
            checkbox_switch.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    checkbox_switch.text = "是"
                    checkbox_switch.setTextColor(holder.itemView.context.resources.getColor(R.color.black))
                    list[position].isRobbing = true
                } else {
                    checkbox_switch.text = "否"
                    checkbox_switch.setTextColor(holder.itemView.context.resources.getColor(R.color.c999999))
                    list[position].isRobbing = false
                }
            }
            val textWatcherListener = object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                    s?.isEmpty()?.let {
                        if (it) {
                            list[position].carNum = 0
                            return
                        }
                    }
                    try {
                        s?.toString()?.toInt()?.let {
                            list[position].carNum = it
                        }
                    } catch (e: Exception) {
                        list[position].carNum = 0
                    }
                }

                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }
            }
            editText.setOnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    editText.addTextChangedListener(textWatcherListener)
                } else {
                    editText.removeTextChangedListener(textWatcherListener)
                }
            }

            when (type) {
                1 -> {
                    ll_goodsName.visibility = View.GONE
                    ll_contractNum.visibility = View.GONE
                    text_delete.text = "删除"
                }
                2 -> {
                    ll_goodsName.visibility = View.VISIBLE
                    ll_contractNum.visibility = View.VISIBLE
                    text_delete.text = "删\n除"
                }
            }
            text_goodsName.text = list[position].mtrL_DESCR ?: ""
            text_contractNum.text = list[position].mtrL_ORDER_NO ?: ""
            text_carrierName.text = list[position].supplieR_NAME ?: ""
            text_delete.setOnClickListener {
                //                if (list.size > 1) {
                list.removeAt(position)
                notifyDataSetChanged()
//                } else {
//                    Toast.makeText(holder.itemView.context, "至少调度一项", Toast.LENGTH_SHORT).show()
//                }
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    fun setCheckBox(visibility: Boolean) {
        this.isCheckBoxVisibility = visibility
        notifyDataSetChanged()
    }

    fun setType(type: Int) {
        this.type = type
    }
}